import contextlib
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from util import *

source_id = 4
RETRIES = Retry(total=3,
                backoff_factor=0.5,
                status_forcelist=[k for k in range(400, 600)])

HOT_URL = "https://i-lq.snssdk.com/api/feed/hotboard_online/v1/"


@contextlib.contextmanager
def request_session():
    s = requests.session()
    try:
        s.mount("http://", HTTPAdapter(max_retries=RETRIES))
        s.mount("https://", HTTPAdapter(max_retries=RETRIES))
        yield s
    finally:
        s.close()


class Toutiao:
    def get_hot_search(self):
        items = []
        resp = None
        try:
            with request_session() as s:
                params = {'category': 'hotboard_online',
                          'count': '50'}
                resp = s.get(HOT_URL, params=params)
                obj = resp.json()
                items = obj['data']
        except:
            logger.exception('get hot search failed')
        return (items, resp)


def toutiao_search():
    tt = Toutiao()
    searches, resp = tt.get_hot_search()
    response_data = []
    for val in searches:
        response_data.append(json.loads(val['content']))
    params = (
        "id", "source_id", "title", "is_hot",  "is_new","is_fei", "create_time", "hot_index","category", "onboard_time", "info_complete")
    create_time = str(int(time.time()))
    all_data_tuple = []
    hot_words = []
    for val in response_data:
        item = {
            "id": int(unique_id()),
            "source_id": source_id,
            "title": val['raw_data']['title'],
            "is_hot": int('hot' == val['raw_data']['tag']),
            "is_new": int('new' == val['raw_data']['tag']),
            "is_fei":0,
            "create_time": create_time,
            "hot_index": val['id'],
            "category":"",
            "onboard_time": str(int(val['behot_time'])),
            "info_complete": 1
        }
        all_data_tuple.append(tuple(item.values()))
        hot_words.append(item)
    flag = insert_hot_words(params, hot_words)
    if flag:
        return hot_words
    else:
        return []


if __name__ == "__main__":
    data_list = toutiao_search()
    if type(data_list) == list and len(data_list) > 0:
        print('头条热词搜索结束')
        baidu_search(data_list)
    elif type(data_list) == list and len(data_list) == 0:
        print('头条热词搜索结果为空')
    else:
        print('百度搜索失败;未进行百度搜索')
